import { UseFilters } from '@nestjs/common';
import { Args, Int, Mutation, Resolver } from '@nestjs/graphql';
import { GrpcFilter } from 'src/common/grpc/grpc.filter';
import { Answer } from './answer.entity';
import { GrpcService } from 'src/common/grpc/grpc.service';
import { User } from 'src/user/user.decorator';
import { UserEntity } from 'src/user/user.entity';

@UseFilters(GrpcFilter)
@Resolver(() => Answer)
export class AnswerResolver {
  constructor(private readonly grpcService: GrpcService) {}

  @Mutation(() => Answer)
  async createAnswer(
    @User() user: UserEntity,
    @Args('questionId', { type: () => Int }) questionId: number,
    @Args('content') content: string,
  ): Promise<Answer> {
    const pbp = await this.grpcService.service
      .createAnswer({ ID: 0, userId: user._id, content, questionId })
      .toPromise();
    const p: Answer = { _id: pbp.ID, ...pbp };
    return p;
  }

  @Mutation(() => Boolean)
  async removeAnswer(@Args('_id') _id: number): Promise<boolean> {
    this.grpcService.service.removeAnswer({ ID: _id });
    return true;
  }
}
